package org.wakeful.sql;

import org.wakeful.query.Query;

/**
 *
 * @author atila
 */
public final class CreateStatement {

   // Example: INSERT INTO wakeful.user (user_id, login, password) VALUES (?, ?, ?)
   private static final String INSERT = Query.INSERT.concat("#schema.#table (#param)").concat(Query.VALUES).concat("(#values)");
   // Example: UPDATE wakeful.user SET login = ?, password = ? WHERE user_id = ?
   private static final String UPDATE = Query.UPDATE.concat("#schema.#table").concat(Query.SET).concat("#param").concat(Query.WHERE).concat("#condition");

   private CreateStatement() {
   }

   /**
    * Generate an Insert SQL Query.
    * @param schema The name of the schema, null to public
    * @param table The table name
    * @param parameters A comma separated parameters, like name, age
    * @return The generated Insert SQL Query, like INSERT INTO wakeful.user (user_id, login, password) VALUES (?, ?, ?)
    */
   public static String toInsertQuery(String schema, String table, String parameters) {
      String query = INSERT;
      if (schema != null) {
         query = query.replaceFirst("#schema", schema);
      } else {
         query = query.replaceFirst("#schema.", "");
      }
      query = query.replaceFirst("#table", table);
      query = query.replaceFirst("#param", parameters);
      // id_usurio, nome, fone
      int numParam = parameters.split(Query.COMMA_).length;
      String values = "";
      for (int i = 0; i < (numParam - 1); i++) {
         values += "?, ";
      }
      values += "?";
      query = query.replaceFirst("#values", values);
      return query;
   }

   /**
    * Generate an Insert SQL Query.
    * @param schema The name of the schema, null to public
    * @param table The table name
    * @param parameters A comma separated parameters, like name, age
    * @param returning What column do you want to retrieve.
    * @return The generated Insert SQL Query, like INSERT INTO wakeful.user (user_id, login, password) VALUES (?, ?, ?)
    */
   public static String toInsertQuery(String schema, String table, String parameters, String returning) {
      String query = toInsertQuery(schema, table, parameters);
      query = query.concat(Query.RETURNING).concat(returning);
      return query;
   }

   /**
    * Generate an Update SQL Query.
    * @param schema The name of the schema, null to public
    * @param table The table name
    * @param parameters A comma separated parameters, like name, age
    * @param condition A comma separated conditions, like user_id, person_id
    * @return The generated Update SQL Query, like UPDATE wakeful.user SET login = ?, password = ? WHERE user_id = ?
    */
   public static String toUpdateQuery(String schema, String table, String parameters, String condition) {
      String query = UPDATE;
      if (schema != null) {
         query = query.replaceFirst("#schema", schema);
      } else {
         query = query.replaceFirst("#schema.", "");
      }
      query = query.replaceFirst("#table", table);
      String[] vParameters = parameters.split(", ");
      String[] vCondition = condition.split(", ");
      String param = "";
      String cond = "";
      for (int i = 0; i < (vParameters.length - 1); i++) {
         param += vParameters[i].concat(Query.EQUALS_QUESTION).concat(Query.COMMA_);
      }
      for (int i = 0; i < (vCondition.length - 1); i++) {
         cond += vCondition[i].concat(Query.EQUALS_QUESTION).concat(Query.COMMA_);
      }
      param += vParameters[vParameters.length - 1].concat(Query.EQUALS_QUESTION);
      cond += vCondition[vCondition.length - 1].concat(Query.EQUALS_QUESTION);
      query = query.replaceFirst("#param", param);
      query = query.replaceFirst("#condition", cond);
      return query;
   }
}

